<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIHeadStart String Helpers
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */

// --------------------------------------------------------------------

/**
 * begins_with returns true if a string begins with a particular set of characters
 *
 * @param string $haystack
 * @param string $needle
 * @return boolean
 */
function begins_with($haystack, $needle)
{
  if (substr($haystack, 0, strlen($needle)) == $needle)
    return true;
  else
    return false;
}

// --------------------------------------------------------------------

/**
 * ends_with returns true if a string ends with a particular set of characters
 *
 * @param string $haystack
 * @param string $needle
 * @return boolean
 */
function ends_with($haystack, $needle)
{
  if (substr($haystack, strlen($haystack)-strlen($needle)) == $needle)
    return true;
  else
    return false;

}

// --------------------------------------------------------------------

/**
 * No Special Characters Helper
 *
 * Check for special characters besides alphanumeric characters or spaces
 * in a string and returns TRUE or FALSE based on whether any were found
 *
 * @param string $string
 * @return boolean
 */
function no_special_chars($string)
{
	//This array defines all of the characters to ALLOW as standard
	//alphanumeric/space
	$allowed = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
									 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', ' ', "'");

	for($x = 0; $x < strlen($string); $x++)
	{
		if ( ! in_array(substr($string, $x, 1), $allowed))
			return FALSE;
	}

	return TRUE;
}

// --------------------------------------------------------------------

/**
 * Formats a vaild XML string and makes it look pretty
 *
 * @link http://recurser.com/articles/2007/04/05/format-xml-with-php/
 * @param string $xml
 * @return string
 */
function format_xml_string($xml)
{
  // add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
  $xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);

  // now indent the tags
  $token      = strtok($xml, "\n");
  $result     = ''; // holds formatted version as it is built
  $pad        = 0; // initial indent
  $matches    = array(); // returns from preg_matches()

  // scan each line and adjust indent based on opening/closing tags
  while ($token !== false) :

    // test for the various tag states

    // 1. open and closing tags on same line - no change
    if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) :
      $indent=0;
    // 2. closing tag - outdent now
    elseif (preg_match('/^<\/\w/', $token, $matches)) :
      $pad--;
    // 3. opening tag - don't pad this one, only subsequent tags
    elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
      $indent=1;
    // 4. no indentation needed
    else :
      $indent = 0;
    endif;

    // pad the line with the required number of leading spaces
    $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
    $result .= $line . "\n"; // add to the cumulative result, with linefeed
    $token   = strtok("\n"); // get the next token
    $pad    += $indent; // update the pad size for subsequent lines
  endwhile;

  return $result;
}

// --------------------------------------------------------------------

/**
 * Replace items in a string, but limit the number of items replaced
 *
 * @param string $search
 * @param string $replace
 * @param string $subject
 * @param int $limit
 * @param boolean $case_sensitive
 * @return string
 */
function str_replace_limited($search, $replace, $subject, $limit = 1, $case_sensitive = TRUE)
{
	$limit = (int) $limit;

	if ($limit <= 0)
		return str_replace($search, $replace, $subject);

	$begin_at = 0;
	$count = 0;

	if ($case_sensitive)
	{
		while (strpos($subject, $search, $begin_at) !== FALSE && $count < $limit)
		{
			$position = strpos($subject, $search, $begin_at);
			$subject = substr($subject, 0, $position) . $replace . substr($subject, $position + strlen($search));
			$begin_at = $position + $replace;
			$count++;
		}
	}
	else
	{
		while (stripos($subject, $search, $begin_at) !== FALSE && $count < $limit)
		{
			$position = stripos($subject, $search, $begin_at);
			$subject = substr($subject, 0, $position) . $replace . substr($subject, $position + strlen($search));
			$begin_at = $position + $replace;
			$count++;
		}
	}

	return $subject;
}

// --------------------------------------------------------------------

/**
 * Replace items in a string, but limit the number of items replaced,
 * case-insensitive
 *
 * @param string $search
 * @param string $replace
 * @param string $subject
 * @param int $limit
 * @return string
 */
function str_ireplace_limited($search, $replace, $subject, $limit = 1)
{
	return str_replace_limited($search, $replace, $subject, $limit, FALSE);
}


// --------------------------------------------------------------------

/**
 * Count the number of bytes of a given string.
 * Input string is expected to be ASCII or UTF-8 encoded.
 * Warning: the function doesn't return the number of chars
 * in the string, but the number of bytes.
 *
 * @author paolo.monsa@gmail.com
 * @link http://us.php.net/manual/en/function.strlen.php#72274
 * @param string $str The string to compute number of bytes
 * @return The length in bytes of the given string.
 */
function str_bytes($str)
{
	// STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT

	// Number of characters in string
	$strlen_var = strlen($str);

	// string bytes counter
	$d = 0;

 /*
	* Iterate over every character in the string,
	* escaping with a slash or encoding to UTF-8 where necessary
	*/
	for ($c = 0; $c < $strlen_var; ++$c) {

			$ord_var_c = ord($str{$d});

			switch (true) {
					case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
							// characters U-00000000 - U-0000007F (same as ASCII)
							$d++;
							break;

					case (($ord_var_c & 0xE0) == 0xC0):
							// characters U-00000080 - U-000007FF, mask 110XXXXX
							// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
							$d+=2;
							break;

					case (($ord_var_c & 0xF0) == 0xE0):
							// characters U-00000800 - U-0000FFFF, mask 1110XXXX
							// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
							$d+=3;
							break;

					case (($ord_var_c & 0xF8) == 0xF0):
							// characters U-00010000 - U-001FFFFF, mask 11110XXX
							// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
							$d+=4;
							break;

					case (($ord_var_c & 0xFC) == 0xF8):
							// characters U-00200000 - U-03FFFFFF, mask 111110XX
							// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
							$d+=5;
							break;

					case (($ord_var_c & 0xFE) == 0xFC):
							// characters U-04000000 - U-7FFFFFFF, mask 1111110X
							// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
							$d+=6;
							break;
					default:
						$d++;
			}
	}
	return $d;
}

/* End of file MY_string_helper.php */
/* Location: ./app/helpers/MY_string_helper.php */